package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcDiscuss;
import com.visizen.wisdomclass.server.model.WcDiscussAttach;
import com.visizen.wisdomclass.server.model.WcDiscussExample;

import java.util.List;

import com.visizen.wisdomclass.server.model.dto.DiscussExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface WcDiscussMapper {
    /**
     * 用户参与的讨论
     *
     * @param userId
     * @param start
     * @param end
     * @return
     */
    @Select(" SELECT\n" +
            "  d.*,\n" +
            "  u.attach_id,\n" +
            "  u.fullname,\n" +
            "  u.area_id\n" +
            " FROM wc_discuss AS d INNER JOIN wc_user AS u\n" +
            "    ON u.user_id = #{userId}\n" +
            " WHERE d.discuss_id IN (SELECT\n" +
            "                         discuss_id\n" +
            "                       FROM wc_discuss_comment\n" +
            "                       WHERE user_id = #{userId} OR discuss_comment_id IN (SELECT\n" +
            "                                                                     discuss_comment_id\n" +
            "                                                                   FROM wc_discuss_comment_reply\n" +
            "                                                                   WHERE user_id = #{userId} OR target_user_id = #{userId}\n" +
            "                                                                   GROUP BY discuss_comment_id)\n" +
            "                       GROUP BY discuss_id)\n" +
            " ORDER BY discuss_time DESC\n" +
            " LIMIT #{start}, #{end};")
    @Results({
            @Result(property = "discuss_id", column = "discuss_id"),
            @Result(property = "userAttachId", column = "attach_id"),
            @Result(property = "userFullname", column = "fullname"),
            @Result(property = "userAreaId", column = "area_id"),
            @Result(property = "attachList", column = "discuss_id", javaType = List.class, many = @Many(select = "selectAttaches")),
            @Result(property = "commentCount", column = "discuss_id", one = @One(select = "selectCommentCount")),
            @Result(property = "praiseCount", column = "discuss_id", one = @One(select = "selectPraiseCount")),
            @Result(property = "seeCount", column = "discuss_id", one = @One(select = "selectSeeCount"))
    })
    List<DiscussExt> joinedDiscussList(@Param("userId") Long userId,
                                       @Param("start") int start,
                                       @Param("end") int end);

    /**
     * 用户创建的讨论
     *
     * @param userId
     * @param start
     * @param end
     * @return
     */
    @Select(" SELECT\n" +
            "  d.*,\n" +
            "  u.attach_id,\n" +
            "  u.fullname,\n" +
            "  u.area_id\n" +
            " FROM wc_discuss AS d, wc_user AS u\n" +
            " WHERE d.user_id=#{userId} AND u.user_id = #{userId}\n" +
            " ORDER BY discuss_time DESC" +
            " LIMIT #{start},#{end}")
    @Results({
            @Result(property = "discuss_id", column = "discuss_id"),
            @Result(property = "userAttachId", column = "attach_id"),
            @Result(property = "userFullname", column = "fullname"),
            @Result(property = "userAreaId", column = "area_id"),
            @Result(property = "attachList", column = "discuss_id", javaType = List.class, many = @Many(select = "selectAttaches")),
            @Result(property = "commentCount", column = "discuss_id", one = @One(select = "selectCommentCount")),
            @Result(property = "praiseCount", column = "discuss_id", one = @One(select = "selectPraiseCount")),
            @Result(property = "seeCount", column = "discuss_id", one = @One(select = "selectSeeCount"))
    })
    List<DiscussExt> createdDiscussList(@Param("userId") Long userId,
                                        @Param("start") int start,
                                        @Param("end") int end);

    /**
     * 获取指定话题的讨论
     *
     * @param topicId
     * @param start
     * @param end
     * @return
     */
    @Select(" SELECT\n" +
            "  d.*,u.attach_id,u.fullname,u.area_id\n" +
            " FROM wc_discuss as d,wc_user as u\n" +
            " WHERE d.discuss_id NOT IN (SELECT\n" +
            "                       discuss_id\n" +
            "                     FROM wc_topic_top\n" +
            "                     WHERE topic_id = #{topicId} and expiration_time>current_time) and u.user_id = d.user_id" +
            "   order by discuss_time desc" +
            " LIMIT #{start},#{end}")
    @Results({
            @Result(property = "discuss_id", column = "discuss_id"),
            @Result(property = "userAttachId", column = "attach_id"),
            @Result(property = "userFullname", column = "fullname"),
            @Result(property = "userAreaId", column = "area_id"),
            @Result(property = "attachList", column = "discuss_id", javaType = List.class, many = @Many(select = "selectAttaches")),
            @Result(property = "commentCount", column = "discuss_id", one = @One(select = "selectCommentCount")),
            @Result(property = "praiseCount", column = "discuss_id", one = @One(select = "selectPraiseCount")),
            @Result(property = "seeCount", column = "discuss_id", one = @One(select = "selectSeeCount"))
    })
    List<DiscussExt> selectDiscussByTopic(@Param("topicId") Long topicId,
                                          @Param("start") int start,
                                          @Param("end") int end);

    @Select(" SELECT\n" +
            "  d.*,u.attach_id,u.fullname,u.area_id\n" +
            " FROM wc_discuss as d,wc_user as u\n" +
            " WHERE d.discuss_id =#{discussId} and u.user_id = d.user_id")
    @Results({
            @Result(property = "discuss_id", column = "discuss_id"),
            @Result(property = "userAttachId", column = "attach_id"),
            @Result(property = "userFullname", column = "fullname"),
            @Result(property = "userAreaId", column = "area_id"),
            @Result(property = "attachList", column = "discuss_id", javaType = List.class, many = @Many(select = "selectAttaches")),
            @Result(property = "commentCount", column = "discuss_id", one = @One(select = "selectCommentCount")),
            @Result(property = "praiseCount", column = "discuss_id", one = @One(select = "selectPraiseCount")),
            @Result(property = "seeCount", column = "discuss_id", one = @One(select = "selectSeeCount"))
    })
    DiscussExt selectDiscussById(@Param("discussId") Long discussId);

    /**
     * 讨论附件列表
     *
     * @param discussId
     * @return
     */
    @Select("select * from wc_discuss_attach where discuss_id=#{discussId}")
    List<WcDiscussAttach> selectAttaches(@Param("discussId") Long discussId);

    /**
     * 讨论的评论数
     *
     * @param discussId
     * @return
     */
    @Select("select COUNT(*) from wc_discuss_comment where discuss_id=#{discussId}")
    int selectCommentCount(@Param("discussId") Long discussId);

    /**
     * 讨论的赞数量
     *
     * @param discussId
     * @return
     */
    @Select("select COUNT(*) from wc_discuss_praise where discuss_id=#{discussId}")
    int selectPraiseCount(@Param("discussId") Long discussId);

    /**
     * 讨论的查看数量
     *
     * @param discussId
     * @return
     */
    @Select("select COUNT(*) from wc_discuss_log_see where discuss_id=#{discussId}")
    int selectSeeCount(@Param("discussId") Long discussId);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcDiscussSqlProvider.class, method = "countByExample")
    int countByExample(WcDiscussExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @DeleteProvider(type = WcDiscussSqlProvider.class, method = "deleteByExample")
    int deleteByExample(WcDiscussExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @Delete({
            "delete from wc_discuss",
            "where discuss_id = #{discuss_id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long discuss_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @Insert({
            "insert into wc_discuss (discuss_id, topic_id, ",
            "user_id, equip_id, ",
            "discuss_time, is_anonymous, ",
            "content)",
            "values (#{discuss_id,jdbcType=BIGINT}, #{topic_id,jdbcType=BIGINT}, ",
            "#{user_id,jdbcType=BIGINT}, #{equip_id,jdbcType=BIGINT}, ",
            "#{discuss_time,jdbcType=TIMESTAMP}, #{is_anonymous,jdbcType=INTEGER}, ",
            "#{content,jdbcType=LONGVARCHAR})"
    })
    int insert(WcDiscuss record);

    @Insert({
            "insert into wc_discuss (discuss_id, topic_id, ",
            "user_id, equip_id, ",
            "discuss_time, is_anonymous, ",
            "content)",
            "values (#{discuss_id,jdbcType=BIGINT}, #{topic_id,jdbcType=BIGINT}, ",
            "#{user_id,jdbcType=BIGINT}, #{equip_id,jdbcType=BIGINT}, ",
            "#{discuss_time,jdbcType=TIMESTAMP}, #{is_anonymous,jdbcType=INTEGER}, ",
            "#{content,jdbcType=LONGVARCHAR})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "discuss_id", keyColumn = "discuss_id")
    int insertAndGenerateKeys(WcDiscuss record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @InsertProvider(type = WcDiscussSqlProvider.class, method = "insertSelective")
    int insertSelective(WcDiscuss record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcDiscussSqlProvider.class, method = "selectByExampleWithBLOBs")
    @Results({
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "discuss_time", property = "discuss_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_anonymous", property = "is_anonymous", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.LONGVARCHAR)
    })
    List<WcDiscuss> selectByExampleWithBLOBs(WcDiscussExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcDiscussSqlProvider.class, method = "selectByExample")
    @Results({
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "discuss_time", property = "discuss_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_anonymous", property = "is_anonymous", jdbcType = JdbcType.INTEGER)
    })
    List<WcDiscuss> selectByExample(WcDiscussExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @Select({
            "select",
            "discuss_id, topic_id, user_id, equip_id, discuss_time, is_anonymous, content",
            "from wc_discuss",
            "where discuss_id = #{discuss_id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "discuss_id", property = "discuss_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "user_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "equip_id", property = "equip_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "discuss_time", property = "discuss_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_anonymous", property = "is_anonymous", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.LONGVARCHAR)
    })
    WcDiscuss selectByPrimaryKey(Long discuss_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") WcDiscuss record, @Param("example") WcDiscussExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussSqlProvider.class, method = "updateByExampleWithBLOBs")
    int updateByExampleWithBLOBs(@Param("record") WcDiscuss record, @Param("example") WcDiscussExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") WcDiscuss record, @Param("example") WcDiscussExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcDiscussSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(WcDiscuss record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @Update({
            "update wc_discuss",
            "set topic_id = #{topic_id,jdbcType=BIGINT},",
            "user_id = #{user_id,jdbcType=BIGINT},",
            "equip_id = #{equip_id,jdbcType=BIGINT},",
            "discuss_time = #{discuss_time,jdbcType=TIMESTAMP},",
            "is_anonymous = #{is_anonymous,jdbcType=INTEGER},",
            "content = #{content,jdbcType=LONGVARCHAR}",
            "where discuss_id = #{discuss_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKeyWithBLOBs(WcDiscuss record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_discuss
     *
     * @mbggenerated
     */
    @Update({
            "update wc_discuss",
            "set topic_id = #{topic_id,jdbcType=BIGINT},",
            "user_id = #{user_id,jdbcType=BIGINT},",
            "equip_id = #{equip_id,jdbcType=BIGINT},",
            "discuss_time = #{discuss_time,jdbcType=TIMESTAMP},",
            "is_anonymous = #{is_anonymous,jdbcType=INTEGER}",
            "where discuss_id = #{discuss_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(WcDiscuss record);
}